/* 
 *  This file is part of hmvcBlog-dashboard.
 *
 *  hmvcBlog-dashboard is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-dashboard is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-dashboard.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblog;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblog.events.in.ShowAddBlogViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.AddBlogEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogSuccessfulUpdatedEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogSuccessulAddedEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogTitleEmptyEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerViewRegistry;

/**
 * 
 *
 * @author jens.meiss
 */
public class AddBlogController extends AControllerViewRegistry<AddBlogView> {

	public void init() {
		
		register(ShowAddBlogViewEvent.class);
		register(BlogSuccessfulUpdatedEvent.class);
		register(BlogTitleEmptyEvent.class);
				
		initView();
		
	}

	public void initView() {
		
		Label labelTitle = new Label(I18N .constants.labelBlogTitle());
		getView().setLabelTitle(labelTitle);
		
		Label labelDescription = new Label(I18N.constants.labelBlogDescription());
		getView().setLabelDescription(labelDescription);
		
		TextBox textBoxTitle = new TextBox();
		getView().setTextBoxTitle(textBoxTitle);
		
		TextArea textAreaDescription = new TextArea();
		getView().setTextAreaDescription(textAreaDescription);
		
		Button buttonAddBlog = new Button(I18N.constants.buttonAddBlog());
		buttonAddBlog.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				addBlog();
			}
		});
		getView().setButtonAddBlog(buttonAddBlog);
		
		Button buttonCancel = new Button(I18N.constants.buttonCancel());
		buttonCancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				getComponent().terminateComponent();
				History.back();
			}
		});
		getView().setButtonCancel(buttonCancel);
		
		HorizontalPanel horizontalPanelButtons = new HorizontalPanel();
		horizontalPanelButtons.add(buttonAddBlog);
		horizontalPanelButtons.add(buttonCancel);
		
		FlexTable felxtableContainer = new FlexTable();
		getView().setFlexTableContainer(felxtableContainer);
		
		felxtableContainer.setWidget(0, 0, labelTitle);
		felxtableContainer.setWidget(0, 1, textBoxTitle);
		felxtableContainer.setWidget(1, 0, labelDescription);
		felxtableContainer.setWidget(1, 1, textAreaDescription);
		felxtableContainer.setWidget(2, 1, horizontalPanelButtons);
		
		
	}
	
	private void addBlog() {
		
		getView().getButtonAddBlog().setEnabled(false);
		
		if (getView().getLabelBlogTitleEmpty() != null) {
			getView().getFlexTableContainer().remove(getView().getLabelBlogTitleEmpty());
		}
		
		BlogNewDTO blogNewDTO = new BlogNewDTO();
		blogNewDTO.setTitle(getView().getTextBoxTitle().getText());
		blogNewDTO.setDescription(getView().getTextAreaDescription().getText());
		
		fireEventGlobal(new AddBlogEvent(blogNewDTO, this));
		
	}

	public void terminate() {
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == ShowAddBlogViewEvent.class) {
			
			fireEventGlobal(new AddApplicationViewEvent(null, getView().getWidget()));
			
		} else if (event.getClass() == BlogSuccessulAddedEvent.class) {
			
			History.newItem(HistoryToken.dashboard);
			getComponent().terminateComponent();
			
		} else if (event.getClass() == BlogTitleEmptyEvent.class) {
			
			blogTitleEmpty();
			getView().getButtonAddBlog().setEnabled(true);
			
		} 
		
	}

	private void blogTitleEmpty() {
		
		Label labelBlogTitleEmpty = null;
		if (getView().getLabelBlogTitleEmpty() == null) {
			labelBlogTitleEmpty = new Label(I18N.constants.labelBlogTitleEmpty());
			getView().setLabelBlogTitleEmpty(labelBlogTitleEmpty);			
		} else {
			labelBlogTitleEmpty = getView().getLabelBlogTitleEmpty();
		}
		
		getView().getFlexTableContainer().setWidget(0, 2, labelBlogTitleEmpty);
		
	}

}
